<template>
  <div>
    <Main :type="'form'">
      <div>
        <Title :title="pageName"></Title>
        <el-row class="phone_wrap">
          <el-col :span="8">
            <div style=" text-align: left; font-size: 14px; margin: 11px 0;">
              手机浏览
            </div>
            <div class="moblieBoxs">
              <img src="http://oss.ztwlxx.cn/shopHTphoneheader.png" />
              <div class="card-wapper">
                <div class="card-show">
                  <div class="card-top">
                    <div class="card-top-show flexSc">
                      <div class="top-left">
                        <div class="title">{{ form.title }}</div>
                        <div class="beizhu">
                          满{{ form.type_condition }}可使用
                        </div>
                      </div>
                      <div class="top-right flexSc">
                        <div class="icon">￥</div>
                        <div v-if="form.type == 0" class="prise">
                          {{ form.type_money }}
                        </div>
                        <div v-else class="prise">
                          {{ form.type_money }}折券
                        </div>
                      </div>
                    </div>
                  </div>
                  <div class="card-bottom flexSc">
                    <div class="bottom-date">
                      有效时间 {{ form.stime }}至 {{ form.etime }}
                    </div>
                    <div class="btn">领取</div>
                  </div>
                </div>
              </div>
            </div>
          </el-col>
          <el-col :span="16">
            <el-form
              ref="form"
              :model="form"
              :rules="rules"
              label-width="150px"
              class="demos-forms"
            >
              <el-form-item label="优惠劵名称" prop="title">
                <el-input
                  v-model="form.title"
                  class="form-input"
                  :disabled="statu"
                  size="medium"
                  placeholder="请输入优惠劵名称"
                ></el-input>
              </el-form-item>

              <el-form-item label="优惠劵类型" prop="type">
                <el-radio-group
                  v-model="form.type"
                  :disabled="statu"
                  @change="typesTag"
                >
                  <el-radio :label="0">满减优惠</el-radio>
                  <el-radio :label="1">折扣优惠</el-radio>
                </el-radio-group>
              </el-form-item>
              <el-form-item v-if="ty" label="减免金额" prop="type_money">
                <el-input-number
                  v-model="form.type_money"
                  :step="1"
                  :min="0"
                  :precision="2"
                  :controls="false"
                  :disabled="statu"
                  size="medium"
                  placeholder="请输入折扣"
                ></el-input-number>
              </el-form-item>
              <el-form-item v-else label="折扣额度" prop="type_money">
                <el-input-number
                  v-model="form.type_money"
                  :step="1"
                  :max="9.9"
                  :min="1"
                  :precision="1"
                  :controls="false"
                  :disabled="statu"
                  size="medium"
                  placeholder="请输入折扣"
                ></el-input-number>
              </el-form-item>
              <el-form-item label="使用条件" prop="type">
                消费金额满&nbsp;&nbsp;
                <el-input-number
                  v-model="form.type_condition"
                  :step="1"
                  :min="0"
                  :precision="0"
                  :controls="false"
                  :disabled="statu"
                  size="medium"
                  placeholder="请输入金额"
                ></el-input-number
                >&nbsp;&nbsp;元
              </el-form-item>
              <el-form-item label="生效时间" prop="Time">
                <el-radio-group v-model="form.type_time" :disabled="statu">
                  <el-radio :label="1">固定时间</el-radio>
                  <el-radio :label="2">领取后生效</el-radio>
                </el-radio-group>
                <el-form-item v-if="form.type_time === 1" prop="withTime">
                  <el-date-picker
                    v-model="form.withTime"
                    format="yyyy-MM-dd"
                    value-format="yyyy-MM-dd"
                    type="daterange"
                    range-separator="至"
                    start-placeholder="开始日期"
                    end-placeholder="结束日期"
                    class="form-input"
                    :disabled="statu"
                  >
                  </el-date-picker>
                </el-form-item>

                <el-form-item v-if="form.type_time === 2" prop="stime">
                  领劵以后&nbsp;&nbsp;
                  <el-input-number
                    v-model="form.stime"
                    :step="1"
                    :min="0"
                    :precision="0"
                    :controls="false"
                    :disabled="statu"
                    size="medium"
                    placeholder="请输入天数"
                  ></el-input-number
                  >&nbsp;&nbsp;天
                </el-form-item>
                <el-form-item v-if="form.type_time === 2">
                  有效天数&nbsp;&nbsp;
                  <el-input-number
                    v-model="form.etime"
                    :step="1"
                    :min="0"
                    :precision="0"
                    :controls="false"
                    :disabled="statu"
                    size="medium"
                    placeholder="请输入天数"
                    style="margin:10px 0;"
                  ></el-input-number
                  >&nbsp;&nbsp;天&nbsp;&nbsp;
                  <span style="color:#909399;font-size:12px;"
                    >如添 0 或 不填 则默认为永久有效</span
                  >
                </el-form-item>
              </el-form-item>
              <el-form-item label="优惠劵库存" prop="stock">
                <el-input-number
                  v-model="form.stock"
                  :step="1"
                  :min="1"
                  :precision="0"
                  :controls="false"
                  class="form-input"
                  :disabled="statu"
                  size="medium"
                  placeholder="请输入优惠劵领取限制"
                ></el-input-number>
              </el-form-item>
              <el-form-item label="优惠劵领取限制" prop="user_limit">
                <el-input-number
                  v-model="form.user_limit"
                  :step="1"
                  :min="form.user_limits"
                  :precision="0"
                  :controls="false"
                  class="form-input"
                  size="medium"
                  placeholder="请输入优惠劵领取限制"
                ></el-input-number
                ><br />
                <span style="color:#909399;font-size:12px;"
                  >每个用户领劵上限，如不填则默认为1</span
                >
              </el-form-item>

              <el-form-item label="适用商品" prop="type_shop">
                <el-radio-group v-model="form.type_shop">
                  <el-radio :label="0">全部商品参与</el-radio>
                  <el-radio :label="1">指定商品参与</el-radio>
                  <!-- <el-radio :label='2'>指定商品不参与<1/el-radio> -->
                </el-radio-group>
                <div v-if="form.type_shop != 0">
                  <shop-new :data-arr="DataArr" @close="close" />
                </div>
              </el-form-item>
            </el-form>
          </el-col>
        </el-row>
      </div>
    </Main>

    <Footer>
      <div v-if="!statu">
        <el-button plain @click="$emit('out', 'list')">取 消</el-button>
        <el-button type="primary" @click="submitForm">立即创建</el-button>
      </div>
      <div v-else>
        <el-button plain @click="$emit('out', 'msg', dataMsg.lg_coupon_id)"
          >取 消</el-button
        >
        <el-button type="primary" @click="editForm">立即修改</el-button>
      </div>
    </Footer>
    <!-- <shop
      :dialog-visible="dialogVisible"
      :data-json="dataJson"
      @close="close"
    /> -->
  </div>
</template>
<script>
import { ADD_COUPON, GET_COUPON } from "../service";

export default {
  components: {
    shopNew: () => import("publicCom/shopNew")
  },
  props: {
    dataMsg: {
      type: Object,
      default: () => {
        return {};
      }
    }
  },
  data() {
    return {
      pageName: "新建优惠劵",
      ty: true,
      dialogVisible: false,
      dataJson: {},
      form: {
        title: "",
        type: 0,
        type_money: "",
        type_condition: "",
        stock: "",
        user_limit: "",
        user_limits: 1,
        type_shop: 0,
        stime: "",
        etime: "",
        Time: "1",
        withTime: [],
        type_time: 1
      },
      zhekou: "",
      apply_start_timeTxt: "",
      apply_end_timeTxt: "",
      rules: {
        title: [
          { required: true, message: "请输入优惠劵名称", trigger: "blur" }
        ],
        Time: [{ required: true, message: "请输入时间", trigger: "blur" }],
        withTime: [{ required: true, message: "请选择时间", trigger: "blur" }],
        stime: [{ required: true, message: "请输入天数", trigger: "blur" }],
        stock: [{ required: true, message: "请输入库存数量", trigger: "blur" }]
      },
      DataArr: [],
      statu: false
    };
  },
  created() {
    if (JSON.stringify(this.dataMsg) !== "{}") {
      this.pageName = "编辑优惠劵";
      this.statu = true;
      this.form = {
        title: this.dataMsg.title,
        type: JSON.parse(this.dataMsg.type),
        type_money: this.dataMsg.type_money,
        type_condition: this.dataMsg.type_condition,
        stock: this.dataMsg.stock,
        user_limit: this.dataMsg.user_limit,
        user_limits: this.dataMsg.user_limit,
        type_shop: JSON.parse(this.dataMsg.type_shop),
        type_time: this.dataMsg.type_time
      };
      if (this.dataMsg.type_time === 1) {
        this.form.withTime = [this.dataMsg.stime, this.dataMsg.etime];
      } else {
        this.form.stime = this.dataMsg.stime;
        this.form.etime = this.dataMsg.etime;
      }
      this.DataArr = this.dataMsg.shop_list;
    } else {
      this.pageName = "新建优惠劵";
      this.statu = false;
    }
  },

  methods: {
    close(e) {
      if (e) {
        this.DataArr = e;
      }
    },
    async submitForm() {
      let applyShop = [];
      if (this.form.type_shop === 0) {
        applyShop = 0;
      } else {
        this.DataArr.forEach(ele => {
          applyShop.push(ele.goods_id);
        });
        applyShop = applyShop.join(",");
      }
      let prams = {
        title: this.form.title,
        stock: this.form.stock,
        type: this.form.type,
        type_money: this.form.type_money,
        type_condition: this.form.type_condition,
        apply_shop: applyShop,
        user_limit: this.form.user_limit,
        type_shop: this.form.type_shop,
        type_time: this.form.type_time,
        stime:
          this.form.type_time === 1 ? this.form.withTime[0] : this.form.stime,
        etime:
          this.form.type_time === 1 ? this.form.withTime[1] : this.form.etime
      };
      await this.$refs["form"].validate(valid => {
        if (valid) {
          ADD_COUPON(prams).then(({ code, msg }) => {
            if (code === 200) {
              this.$emit("out", "list");
              this.$message.success(msg);
            } else {
              this.$message.error(msg);
            }
          });
        } else {
          return false;
        }
      });
    },
    async editForm() {
      let applyShop = [];
      if (this.form.type_shop === 0) {
        applyShop = 0;
      } else {
        this.DataArr.forEach(ele => {
          applyShop.push(ele.goods_id);
        });
        applyShop = applyShop.join(",");
      }
      GET_COUPON({
        applyShop,
        type: 1,
        lg_coupon_id: this.dataMsg.lg_coupon_id,
        user_limit: this.form.user_limit
      }).then(res => {
        let { code, msg } = res;
        if (code === 200) {
          this.$emit("out", "msg", this.dataMsg.lg_coupon_id);
        }
        this.$message({
          message: msg,
          type: code === 200 ? "success" : "error"
        });
      });
    },
    typesTag(e) {
      e === 0 ? (this.ty = true) : (this.ty = false);
      this.form.type_money = "";
    }
  }
};
</script>
<style lang="scss" scoped>
.phone_wrap {
  padding: 0 10px;
}
.moblieBoxs {
  width: 378px;
  border: 1px solid #f5f5f5;
  height: 600px;
  background: #f5f5f5;
}

.counp-Type-b {
  padding: 34px 16px;
  display: flex;
  align-items: center;
  color: #fff;
  margin-top: 5px;
  flex-wrap: wrap;
  margin-left: 4%;
  margin-bottom: 6px;
  .couponStyle-b {
    width: 100px;
    height: 87px;
    border-radius: 10px 0px 0px 10px;
    background: #ff3e3e;
    text-align: center;
    .lefts-b {
      margin-top: 10%;
      .top-b {
        font-size: 13px;
        line-height: 26px;
      }
      .cent-b {
        line-height: 22px;
      }
      .bottom-b {
        font-size: 12px;
        line-height: 9px;
      }
    }
    .use_condition {
      margin-top: 15%;
    }
  }
  .couponStyle-b-c {
    width: 150px;
    height: 87px;
    background: #ff3e3e;
    text-align: center;
    position: relative;
    .lefts-b {
      margin-top: 10%;
      .zhe {
        width: 130px;
        font-size: 14px;
        line-height: 33px;
      }

      .data {
        width: 125px;
        font-size: 11px;
        line-height: 13px;
      }
    }
    .linel {
      width: 2px;
      height: 55px;
      background: #fff;
      position: absolute;
      top: 19px;
      left: -7px;
    }
  }
  .couponStyle-r-b {
    height: 85px;
    width: 35px;
    border-radius: 0px 50px 50px 0px;
    background: #ff3e3e;
    position: relative;
    .rights-b {
      position: absolute;
      top: 0;
      left: -23px;
      text-align: center;
      img {
        width: 95px;
        height: 77px;
        border-radius: 5px;
        margin-top: 4px;
        margin-left: -18%;
      }

      span {
        position: absolute;
        top: 37%;
        left: 20px;
        font-size: 12px;
        color: #000;
      }
    }
  }
}

.card-wapper {
  width: 100%;
  padding: 11px 13px 0 15px;
  box-sizing: border-box;
  margin-bottom: 9px;
  .flexSc {
    display: flex;
    justify-content: space-between;
    align-items: center;
  }
  .card {
    width: 100%;
    height: 127px;
    background: skyblue;
  }
  .card-show {
    width: 100%;
    height: 127px;
    background: url("https://oss.ztwlxx.cn/jsynewshop%E4%BC%9A%E5%91%98%E5%8D%B7.png")
      no-repeat 0 50%;
    background-size: 100%;
  }
  .card-top {
    width: 100%;
    height: 72px;
    padding: 14px 9px 10px 18px;
    box-sizing: border-box;
  }
  .card-top-show {
    width: 100%;
    height: 100%;
  }
  .beizhu {
    height: 17px;
    font-family: PingFangSC-Regular;
    font-size: 14px;
    line-height: 27.3px;
    color: #f2dbac;
    margin-top: 6px;
  }
  .title {
    height: 13px;
    font-family: PingFangSC-Regular;
    font-size: 20px;
    line-height: 27.3px;
    color: #333;
  }
  .date {
    height: 18px;
    font-family: PingFangSC-Regular;
    font-size: 14px;
    line-height: 25.8px;
    color: #666;
    margin-top: 32px;
  }
  .icon {
    width: 18px;
    height: 20px;
    font-family: PingFangSC-Regular;
    font-size: 14px;
    color: #f2dbac;
  }
  .prise {
    height: 75px;
    line-height: 75px;
    font-family: PingFangSC-Regular;
    font-size: 46px;
    color: #f2dbac;
  }
  .bottom-date {
    font-size: 14px;
    height: 17px;
    font-family: PingFangSC-Regular;
    line-height: 25.8px;
    color: #666;
  }
  .card-bottom {
    width: 100%;
    height: 55px;
    padding: 18px 17px 15px 18px;
    box-sizing: border-box;
  }
  .guoqi-btn {
    border: solid 1px #9d9e9d;
    color: #9e9e9d;
  }
  .btn,
  .guoqi-btn {
    width: 61px;
    height: 26px;
    border: solid 1px #c09d53;
    text-align: center;
    font-size: 15px;
    line-height: 26px;
    color: #c09d53;
    border-radius: 7.5px;
  }
}
</style>
